package com.edgar.server;

import com.edgar.protocol.MyDecoder;
import com.edgar.protocol.MyEncoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslHandler;

import javax.net.ssl.SSLEngine;

/**
 * @author liqs
 * @version 1.0
 * @date 2020/12/30 11:13
 */
public class NettySocketServer {

    private static SslHandler sslHandler = null ;

    private EventLoopGroup bossGroup = null ;

    private EventLoopGroup workerGroup = null ;
    private int port;

    public NettySocketServer(int port){
        this.port = port;
    }

    public void start(){
        bossGroup = new NioEventLoopGroup(1) ;
        workerGroup = new NioEventLoopGroup() ;
        try{
            //NIO服务的辅助启动类
            ServerBootstrap serverStrap = new ServerBootstrap() ;
            serverStrap.group(bossGroup , workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 5 * 60)
                    .handler(new LoggingHandler(LogLevel.DEBUG)) //设置父类handler
                    .childHandler(new ChannelInitializer<SocketChannel>() {

                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pie = socketChannel.pipeline() ;
                            pie.addLast("decoder" , new MyDecoder()) ; // 解码
                            pie.addLast("encoder" , new MyEncoder()) ; // 编码
                            pie.addLast("handler" , new NettySocketSSLHandler()) ;
                            SSLEngine engine = ContextSSLFactory.getSslContext2().createSSLEngine();
                            engine.setUseClientMode(false);
                            engine.setNeedClientAuth(true);
                            pie.addFirst("ssl", new SslHandler(engine));
                        }

                    });
            serverStrap.bind(this.port).sync();
            System.out.println("服务已开启");
        }catch(Exception e){
            e.printStackTrace() ;
            bossGroup.shutdownGracefully() ;
            workerGroup.shutdownGracefully() ;
        }

    }

    private SslHandler getSslHandler(){
        if(sslHandler == null ){
            SSLEngine sslEngine = ContextSSLFactory.getSslContext().createSSLEngine() ;
//            sslEngine.setUseClientMode(false) ;
            sslEngine.setUseClientMode(true) ;
            //false为单向认证，true为双向认证
            sslEngine.setNeedClientAuth(true) ;
            sslHandler = new SslHandler(sslEngine);
        }
        return sslHandler ;
    }

}
